package com.csyf.manager.service.impl;

import com.csyf.manager.dao.SysRoleMapper;
import com.csyf.manager.dao.SysRoleMenuMapper;
import com.csyf.manager.entity.SysRole;
import com.csyf.manager.entity.SysRoleMenu;
import com.csyf.manager.service.SysRoleService;
import com.csyf.manager.utils.ResultEntity;
import com.csyf.manager.utils.ResultUtli;
import com.csyf.manager.utils.page.JqGridVo;
import com.csyf.manager.utils.page.PageUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @Author: casper
 * @Description: 角色ServiceImpl
 * @Date: Created in 2018/11/19 11:39
 */
@Slf4j
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 根据用户id查询角色集合
     * @param userId
     * @return
     */
    @Override
    public Set<String> findRoleByUserId(Long userId) {
        return sysRoleMapper.findRoleByUserId(userId);
    }

    /**
     * 角色列表
     * @param vo
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public JqGridVo queryRoleList(SysRole vo, Integer pageIndex, Integer pageSize) {

        PageHelper.startPage(pageIndex, pageSize);
        PageHelper.orderBy("create_time");

        Example example = new Example(SysRole.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andEqualTo(criteria, "roleName", vo.getRoleName());
        PageUtils.andEqualTo(criteria, "status", "1");

        List<SysRole> select = sysRoleMapper.selectByExample(example);
        return PageUtils.pasrJqGrid(select);
    }

    /**
     * 新增角色
     * @param roleForm
     * @return
     */
    @Override
    public int addRole(SysRole roleForm) {
        roleForm.setCreateTime(new Date());
        roleForm.setModifyTime(new Date());
        return sysRoleMapper.insert(roleForm);
    }

    /**
     * 批量更新角色状态
     * @param roleIds
     * @param status
     * @return
     */
    @Override
    public int updateStatusBatch(List<String> roleIds, Integer status) {
        Map<String,Object> params = new HashMap<String,Object>(2);
        params.put("roleIds", roleIds);
        params.put("status", status);
        return sysRoleMapper.updateStatusBatch(params);
    }

    /**
     * 根据角色id查询角色信息
     * @param roleId
     * @return
     */
    @Override
    public SysRole findById(Long roleId) {
        return sysRoleMapper.findByRoleId(roleId);
    }

    /**
     * 根据角色id更新角色
     * @param role
     * @return
     */
    @Override
    public int updateByRoleId(SysRole role) {
        Map<String,Object> params  = new HashMap<>(3);
        params.put("roleName",role.getRoleName());
        params.put("remark",role.getRemark());
        params.put("roleId",role.getRoleId());
        return sysRoleMapper.updateByRoleId(params);
    }

    /**
     * 根据角色id保存分配权限
     * @param roleId
     * @param menuIdsList
     */
    @Override
    public ResultEntity addAssignMenu(Long roleId, List<String> menuIdsList) {
        try {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(roleId);
            sysRoleMenuMapper.delete(roleMenu);

            for (String menuId : menuIdsList) {
                roleMenu.setMenuId(menuId);
                sysRoleMenuMapper.insert(roleMenu);
            }
            return ResultUtli.DEFAULT_SUCCESS("分配菜单成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtli.DEFAULT_ERROR("分配菜单失败！");
        }
    }

    /**
     * 根据条件查询角色列表
     * @param sysRole
     * @return
     */
    @Override
    public List<SysRole> selectRoles(SysRole sysRole) {
        return sysRoleMapper.selectRoles(sysRole);
    }
}
